package com.cssy.yd.logic.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.byd.acegiext.domain.User;
import com.cssy.yd.logic.dao.LogicPoQuoteDAO;
import com.cssy.yd.logic.dao.LogicPoQuoteItemDAO;
import com.cssy.yd.logic.entity.LogicPoItemDO;
import com.cssy.yd.logic.entity.LogicPoQuoteDO;
import com.cssy.yd.logic.entity.LogicPoQuoteItemDO;
import com.cssy.yd.logic.service.ILogicPoQuoteService;
import com.plat.exception.SyBaseException;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.DateUtils;
import com.plat.util.GlobalResLoader;
import com.plat.util.IDCodeTypeUtil;
import com.plat.util.SessionUtil;
import com.yuendong.sy.po.code.SyCode;
import com.yuendong.sy.service.code.SyCodeType;
import com.yuendong.sy.service.code.impl.SyCodeService;

@Service("logicPoQuoteService")
public class LogicPoQuoteServiceImpl implements ILogicPoQuoteService {

	@Resource(name="logicPoQuoteDAO")
	private LogicPoQuoteDAO	logicPoQuoteDAO;
	
	@Resource(name="logicPoQuoteItemDAO")
	private LogicPoQuoteItemDAO logicPoQuoteItemDAO;
	
	@Resource(name = "syCodeService")
	private SyCodeService syCodeService;
	@Override
	public SearchResult findListByExample(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs = new SearchResult(scs);
		List<LogicPoQuoteDO> list = logicPoQuoteDAO.find(scs);
		rs.setRecordCount(logicPoQuoteDAO.count(scs));
		rs.setResults(list);
		return rs;
	}

	@Override
	public Integer delete(String id, User user) throws SyBaseException {
		// TODO Auto-generated method stub
		logicPoQuoteDAO.deleteLogicPoQuoteDOByPrimaryKey(id);
		return null;
	}

	@Override
	public Integer update(LogicPoQuoteDO t, User user) throws SyBaseException {
		t.setDuuser(user.getName());
		t.setDutime(new Date());
		return logicPoQuoteDAO.updateLogicPoQuoteDO(t);
	}

	@Override
	public LogicPoQuoteDO save(LogicPoQuoteDO t, User user)
			throws SyBaseException {
		t.setDcruser(user.getName());
		t.setDctime(new Date());
		t.setId(IDCodeTypeUtil.getId());
		logicPoQuoteDAO.insertLogicPoQuoteDO(t);
		return t;
	}

	@Override
	public LogicPoQuoteDO findListById(String id) throws SyBaseException {
		
		return logicPoQuoteDAO.findLogicPoQuoteDOByPrimaryKey(id);
	}

	@Override
	public void poQuoteSave(HttpServletRequest request,
			LogicPoQuoteDO logicPoQuoteDO) throws SyBaseException {
		try {
		String itemdata =request.getParameter("itemdata");
		String itemdel =request.getParameter("itemdelete");
		Integer type =Integer.parseInt(request.getParameter("type"));
		logicPoQuoteDO.setQuoteDate(DateUtils.stringToDate(request.getParameter("quoteDate")));
		logicPoQuoteDO.setDeliveryTime(DateUtils.stringToDate(request.getParameter("deliveryTime")));
		logicPoQuoteDO.setPeriodOfValidity(DateUtils.stringToDate(request.getParameter("periodOfValidity")));
		String bhid=request.getParameter("id");
		List<LogicPoQuoteItemDO> quotiem = JSONArray.parseArray(itemdata,
				LogicPoQuoteItemDO.class);
		if(9==type){
			LogicPoQuoteDO lQuoteDO = logicPoQuoteDAO.findLogicPoQuoteDOByPrimaryKey(bhid);
			lQuoteDO.setBillstatus(0);
			this.update(lQuoteDO,  SessionUtil.getUser(request));
		}else{
				if(type ==0 || type==1){
					logicPoQuoteDO.setBillstatus(type);
					if(logicPoQuoteDO.getId() != null && !"".equals(logicPoQuoteDO.getId())){
						logicPoQuoteDO.setVerifyPerson(SessionUtil.getUser(request).getName());
						logicPoQuoteDO.setDuuser(SessionUtil.getUser(request).getName());
						logicPoQuoteDO.setDutime(new Date());
						int rs = logicPoQuoteDAO.updateLogicPoQuoteDO(logicPoQuoteDO);
						if(rs==1){
							if (itemdel != null && !"".equals(itemdel)) {
								List<LogicPoItemDO> quotiemdel = JSONArray.parseArray(itemdel, LogicPoItemDO.class);
								for (int i = 0; i < quotiemdel.size(); i++) {
									if (quotiemdel.get(i).getId() != null&& !"".equals(quotiemdel.get(i).getId())) {
										logicPoQuoteItemDAO.deleteLogicPoQuoteItemDOByPrimaryKey(quotiemdel.get(i).getId());
									}
								}
							}
							for (int i = 0; i < quotiem.size(); i++) {
								if (quotiem.get(i).getId() != null&& !"".equals(quotiem.get(i).getId())) {
									logicPoQuoteItemDAO.updateLogicPoQuoteItemDO(quotiem.get(i));
								} else {
									quotiem.get(i).setQuoteId(logicPoQuoteDO.getId());
									quotiem.get(i).setId(IDCodeTypeUtil.getId());
									logicPoQuoteItemDAO.insertLogicPoQuoteItemDO(quotiem.get(i));
								}
							}
						}
					}else{
						String id = IDCodeTypeUtil.getId();
						logicPoQuoteDO.setCode(syCodeService.getCode("BJ","BJ",SyCode.DATE_YYMM,5));
						logicPoQuoteDO.setDcruser(SessionUtil.getUser(request).getName());
						logicPoQuoteDO.setDctime(new Date());
						logicPoQuoteDO.setId(id);
						logicPoQuoteDAO.insertLogicPoQuoteDO(logicPoQuoteDO);
						if (quotiem.size() > 0){
							for (int i = 0;i < quotiem.size();i++){
								quotiem.get(i).setId(IDCodeTypeUtil.getId());
								quotiem.get(i).setQuoteId(id);
								quotiem.get(i).setDctime(new Date());
								quotiem.get(i).setDcruser(SessionUtil.getUser(request).getName());
								logicPoQuoteItemDAO.insertLogicPoQuoteItemDO(quotiem.get(i));
							}
						}
					}
				}else{
					LogicPoQuoteDO lp = new LogicPoQuoteDO();
					lp.setId(logicPoQuoteDO.getId());
					lp.setBillstatus(type);
					logicPoQuoteDAO.updateLogicPoQuoteDO(lp);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new SyBaseException(GlobalResLoader.getOpfailed()
					+ e.getMessage());
		}
	}

	@Override
	public LogicPoQuoteItemDO findPrice(Map<String, Object> map)
			throws SyBaseException {
		// TODO Auto-generated method stub
		return logicPoQuoteDAO.findPrice(map);
	}

	@Override
	public Integer deleteBizRemoved(String id) throws SyBaseException {
		// TODO Auto-generated method stub
		return null;
	}

}
